<?php

//tree.json表

require 'database.php';

$tree_____=file_get_contents("tree/tree.json");
file_put_contents("tree.json",$tree_____);
$___tree___=file_get_contents("tree.json");
$___tree___=json_decode($___tree___,1);
$___order___=file_get_contents("order.json");
$___order___=json_decode($___order___,1);
for ($i = 0; $i < count($___order___); $i++) {
    if(!empty($___tree___[($___order___[$i]["tree_id"])-1]["tree"][($___order___[$i]["pid"])-1]["order"])){

        for ($a = 0; $a < count($___tree___[($___order___[$i]["tree_id"])-1]["tree"][($___order___[$i]["pid"])-1]["order"]); $a++) {
            for ($b = 0; $b < count($___tree___[($___order___[$i]["tree_id"])-1]["tree"][($___order___[$i]["pid"])-1]["order"]); $b++) {
                if($___tree___[($___order___[$i]["tree_id"])-1]["tree"][($___order___[$i]["pid"])-1]["order"][$a]["id"]==$___tree___[($___order___[$i]["tree_id"])-1]["tree"][($___order___[$i]["pid"])-1]["order"][$b]["id"]){
                    $___order___[$i]["del"]=1;
                }
            }
        }
    }
}
for ($i = 0; $i < count($___order___); $i++) {
    if(!empty($___tree___[($___order___[$i]["tree_id"])-1]["tree"][($___order___[$i]["pid"])-1]["order"])){
        if($___order___[$i]["del"]!=1){
            $___order___[$i]["del"]=0;
            $___tree___[($___order___[$i]["tree_id"])-1]["tree"][($___order___[$i]["pid"])-1]["order"][]=$___order___[$i];
        }
    }else{
        if($___order___[$i]["del"]!=1){
            $___order___[$i]["del"]=0;
            $___tree___[($___order___[$i]["tree_id"])-1]["tree"][($___order___[$i]["pid"])-1]["order"][0]=$___order___[$i];
        }
    }
}
$___tree=json_encode($___tree___,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
$___r___=  file_put_contents("tree.json",$___tree);
if($___r___){
    return json([
        "code"=>200,
        "msg"=>"successful"
    ]);
}else{
    return json([
        "code"=>-1,
        "msg"=>"fuiled"
    ]);
}